package chenji.StrongFossil.listeners;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;

import chenji.StrongFossil.Main;

public class PunchListener implements Listener {
	@EventHandler
	public void onClick(InventoryClickEvent e) {
		Inventory ClickedInventory = e.getClickedInventory();
		Player p = (Player) e.getWhoClicked();
		if (!(Main.getPunchManager().containPunchInventory(p))) {
			return;
		}
		if(ClickedInventory==null){
			return;
		}
		if (ClickedInventory.equals(p.getInventory())) {
			return;
		}
		int slot = e.getSlot();
		if (slot == 20 || slot ==49 || slot == 47 || slot == 46) {
			Main.getPunchManager().refreshPunchInventory(p);
		} else {
			e.setCancelled(true);
		}
		if (slot == 52 || slot == 51) {
			Main.getPunchManager().refreshPunchInventory(p);
			p.updateInventory();
		}
		return;

	}

	@EventHandler
	public void close(InventoryCloseEvent e) {
		Player p = (Player) e.getPlayer();
		if (Main.getPunchManager().containPunchInventory(p)) {
			Main.getPunchManager().close(Main.getPunchManager().getPunchInventory(p));
		}
	}
}
